using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace WisdriContactsAndroid
{
    using System.Security.Cryptography;

    [Activity(Label = "激活界面")]
    public class Active : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Create your application here
            SetContentView(Resource.Layout.Active);


            var buttonActive = FindViewById<Button>(Resource.Id.buttonActive);
            var editTextActiveCode = FindViewById<TextView>(Resource.Id.editTextActiveCode);

            var preference = GetSharedPreferences("settings", FileCreationMode.WorldWriteable);
            var editor = preference.Edit();

            buttonActive.Click += async (sender, e) =>
                {
                    try
                    {
                        string dateSeed = DateTime.Now.ToString("yyyyMM");

                        //建立SHA1对象
                        SHA1 sha = new SHA1Managed();

                        //将mystr转换成byte[]
                        byte[] dataToHash = UTF8Encoding.UTF8.GetBytes(dateSeed);

                        //Hash运算
                        byte[] dataHashed = sha.ComputeHash(dataToHash);

                        //将运算结果转换成string
                        string hash = BitConverter.ToString(dataHashed).Replace("-", "");

                        var key = hash.Substring(1, 5);

                        if (editTextActiveCode.Text.Trim() == key)
                        {
                            editor.PutBoolean("IsActived", true);
                            editor.PutBoolean("IsAdmin", false);
                            editor.Commit();
                            new AlertDialog.Builder(this).SetMessage("激活成功").Show();

                            //App.ViewModel.LoadData();
                            //NavigationService.Navigate(new Uri("/DepartmentPage.xaml", UriKind.Relative));
                        }
                        else if (editTextActiveCode.Text.Trim() == "liqi")
                        {
                            editor.PutBoolean("IsActived", true);
                            editor.PutBoolean("IsAdmin", true);
                            editor.Commit();
                            new AlertDialog.Builder(this).SetMessage("激活管理员模式成功").Show();
                            //App.ViewModel.LoadData();
                            //NavigationService.Navigate(new Uri("/DepartmentPage.xaml", UriKind.Relative));
                        }
                        else
                        {
                            editor.PutBoolean("IsActived", false);
                            editor.PutBoolean("IsAdmin", false);
                            editor.Commit();
                            new AlertDialog.Builder(this).SetMessage("激活失败").Show();
                        }
                    }
                    catch (Exception ex)
                    {
                        new AlertDialog.Builder(this).SetMessage(ex.Message).Show();
                    }
                };

        }
    }
}

